package com.zmzncs.lmtc.module.system.controller;


import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.system.service.RolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Api(tags = "角色权限关联")
@RestController
@RequestMapping(value="/system/role-permission")
@Validated
public class RolePermissionController {

    @Autowired
    RolePermissionService rolePermissionService;

    @ApiOperation(value = "分配权限")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
        @ApiImplicitParam(paramType = "query", name = "roleId", value = "角色id", required = true),
        @ApiImplicitParam(paramType = "query", name = "permissionIds", value = "权限id数组", required = true)
    })
    @PostMapping(value = "assignsPermission")
    public Rest assignsPermission(@NotNull Long roleId, @NotNull Long[] permissionIds){
        return rolePermissionService.assignsPermission(roleId, permissionIds);
    }

    @ApiOperation(value = "根据角色id查询角色权限id数组")
    @GetMapping(value = "getPermissionIdsByRoleId")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query", name = "roleId", value = "角色id", required = true),
    })
    public Rest getPermissionIdsByRoleId(@NotNull Long roleId) {
        Long[] permissionIds = rolePermissionService.getPermissionIdsByRoleId(roleId);
        return RestUtil.success(permissionIds);
    }

}
